package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelRuleEntity;
import com.dragon.modules.yuegou.enums.code.MarketingCode;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleBargainStatus;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleGroupStatus;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleIsDefault;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleStatus;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelStatus;
import com.dragon.modules.yuegou.mapper.merchant.MerchantLevelMapper;
import com.dragon.modules.yuegou.model.merchant.MerchantLevelRule;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelRuleQuery;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelRuleService;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 商户等级 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-23 15:27:57
 * @version 2.0
 */
@Service("merMerchantLevelService")
public class MerchantLevelServiceImpl
		extends ServiceImpl<MerchantLevelMapper, MerchantLevelEntity>
		implements MerchantLevelService {

	@Autowired
	private MerchantLevelRuleService merchantLevelRuleService;

	@Override
	public MerchantLevelRule getLevel(String merchantId) {
		MerchantLevelQuery query = new MerchantLevelQuery();
		query.setMerchantId(merchantId);
		query.setStatus(MerchantLevelStatus.PAID);
		
		MerchantLevelEntity merchantLevel = getOne(query.toQueryWrapper());
		MerchantLevelRuleEntity merchantLevelRule = null;
		if (merchantLevel != null && merchantLevel.getStartTime().isBefore(LocalDateTime.now()) && merchantLevel.getEndTime().isAfter(LocalDateTime.now())) {
			merchantLevelRule = merchantLevelRuleService.getById(merchantLevel.getLevelId());
		}else {
			MerchantLevelRuleQuery levelRuleQuery = new MerchantLevelRuleQuery();
			levelRuleQuery.setIsDefault(MerchantLevelRuleIsDefault.YES);
			levelRuleQuery.setStatus(MerchantLevelRuleStatus.DISABLE);
			merchantLevelRule = merchantLevelRuleService.getOne(levelRuleQuery.toQueryWrapper());
		}
		return new MerchantLevelRule(merchantLevelRule);
	}

	@Override
	public void checkMerchantLevel(String merchantId ,String activityType) {
		MerchantLevelRule levelRule = getLevel(merchantId);

		if ("help".equals(activityType) && MerchantLevelRuleBargainStatus.OFF.equals(levelRule.getBargainStatus())) {
			throw new DragonException(MarketingCode.BARGAIN_STATUS_OFF);
		} else if ("groupon".equals(activityType) && MerchantLevelRuleGroupStatus.OFF.equals(levelRule.getGroupStatus())) {
			throw new DragonException(MarketingCode.GROUPON_STATUS_OFF);
		}
	}
}